<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>分类: 技术教程 | 小小程序员</title><meta name="author" content="十一星野"><meta name="copyright" content="十一星野"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="ELK 即 Elasticsearch、Logstash、Kibana,组合起来可以搭建线上日志系统，本文主要讲解使用 ELK 来收集 SpringBoot 应用产生的日志。  #学习前需要了解的内容 开发者必备Docker命令 使用Docker Compose部署SpringBoot应用 SpringBoot应用中使用AOP记录接口访问日志  #ELK中各个服务的作用 Elasticsearc">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringBoot应用整合ELK实现日志收集">
<meta property="og:url" content="https://ko25891wan.gitlab.io/2024/01/27851c2fc81c.html">
<meta property="og:site_name" content="小小程序员">
<meta property="og:description" content="ELK 即 Elasticsearch、Logstash、Kibana,组合起来可以搭建线上日志系统，本文主要讲解使用 ELK 来收集 SpringBoot 应用产生的日志。  #学习前需要了解的内容 开发者必备Docker命令 使用Docker Compose部署SpringBoot应用 SpringBoot应用中使用AOP记录接口访问日志  #ELK中各个服务的作用 Elasticsearc">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png">
<meta property="article:published_time" content="2024-01-21T09:30:08.000Z">
<meta property="article:modified_time" content="2024-02-02T14:50:42.835Z">
<meta property="article:author" content="十一星野">
<meta property="article:tag" content="宅男,热血">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://ko25891wan.gitlab.io/2024/01/27851c2fc81c.html"><link rel="preconnect" href="//fastly.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.staticfile.org/fancyapps-ui/5.0.32/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":1,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.staticfile.org/egjs-infinitegrid/4.11.0/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '分类: 技术教程',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-02 22:50:42'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (false) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">120</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-tools"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/md_editor/"><i class="fa-fw fas fa-pen"></i><span> MDEditor_my</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="小小程序员"><span class="site-name">小小程序员</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-tools"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/md_editor/"><i class="fa-fw fas fa-pen"></i><span> MDEditor_my</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">SpringBoot应用整合ELK实现日志收集</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-01-21T09:30:08.000Z" title="发表于 2024-01-21 17:30:08">2024-01-21</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-02T14:50:42.835Z" title="更新于 2024-02-02 22:50:42">2024-02-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/">技术教程</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/mall/">mall</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/mall/%E5%8F%82%E8%80%83%E7%AF%87/">参考篇</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.2k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>5分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="SpringBoot应用整合ELK实现日志收集"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><blockquote>
<p>ELK 即 Elasticsearch、Logstash、Kibana,组合起来可以搭建线上日志系统，本文主要讲解使用 ELK 来收集 SpringBoot 应用产生的日志。</p>
</blockquote>
<h2 id="学习前需要了解的内容"><a href="#学习前需要了解的内容" class="headerlink" title="#学习前需要了解的内容"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E5%AD%A6%E4%B9%A0%E5%89%8D%E9%9C%80%E8%A6%81%E4%BA%86%E8%A7%A3%E7%9A%84%E5%86%85%E5%AE%B9">#</a>学习前需要了解的内容</h2><ul>
<li><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/docker_command.html">开发者必备Docker命令</a></li>
<li><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/docker_compose.html">使用Docker Compose部署SpringBoot应用</a></li>
<li><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/aop_log.html">SpringBoot应用中使用AOP记录接口访问日志</a></li>
</ul>
<h2 id="ELK中各个服务的作用"><a href="#ELK中各个服务的作用" class="headerlink" title="#ELK中各个服务的作用"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#elk%E4%B8%AD%E5%90%84%E4%B8%AA%E6%9C%8D%E5%8A%A1%E7%9A%84%E4%BD%9C%E7%94%A8">#</a>ELK中各个服务的作用</h2><ul>
<li>Elasticsearch:用于存储收集到的日志信息；</li>
<li>Logstash:用于收集日志，SpringBoot应用整合了Logstash以后会把日志发送给Logstash,Logstash再把日志转发给Elasticsearch；</li>
<li>Kibana:通过Web端的可视化界面来查看日志。</li>
</ul>
<h2 id="使用Docker-Compose-搭建ELK环境"><a href="#使用Docker-Compose-搭建ELK环境" class="headerlink" title="#使用Docker Compose 搭建ELK环境"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E4%BD%BF%E7%94%A8docker-compose-%E6%90%AD%E5%BB%BAelk%E7%8E%AF%E5%A2%83">#</a>使用Docker Compose 搭建ELK环境</h2><h3 id="需要下载的Docker镜像"><a href="#需要下载的Docker镜像" class="headerlink" title="#需要下载的Docker镜像"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E9%9C%80%E8%A6%81%E4%B8%8B%E8%BD%BD%E7%9A%84docker%E9%95%9C%E5%83%8F">#</a>需要下载的Docker镜像</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker pull elasticsearch:6.4.0</span><br><span class="line">docker pull logstash:6.4.0</span><br><span class="line">docker pull kibana:6.4.0</span><br></pre></td></tr></table></figure>

<h3 id="搭建前准备"><a href="#搭建前准备" class="headerlink" title="#搭建前准备"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E6%90%AD%E5%BB%BA%E5%89%8D%E5%87%86%E5%A4%87">#</a>搭建前准备</h3><ul>
<li>elasticsearch 启动成功需要特殊配置，具体参考<a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/JYkvdub9DP5P9ULX4mehUw">mall在Linux环境下的部署（基于Docker Compose）open in new window</a>中的elasticsearch部分；</li>
<li>docker-compose.yml文件地址:<a target="_blank" rel="noopener" href="https://github.com/macrozheng/mall-learning/blob/master/mall-tiny-elk/src/main/docker/docker-compose.yml">https://github.com/macrozheng/mall-learning/blob/master/mall-tiny-elk/src/main/docker/docker-compose.yml</a></li>
<li>logstash-springboot.conf配置文件地址：<a target="_blank" rel="noopener" href="https://github.com/macrozheng/mall-learning/blob/master/mall-tiny-elk/src/main/docker/logstash-springboot.conf">https://github.com/macrozheng/mall-learning/blob/master/mall-tiny-elk/src/main/docker/logstash-springboot.conf</a></li>
</ul>
<h3 id="开始搭建"><a href="#开始搭建" class="headerlink" title="#开始搭建"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E5%BC%80%E5%A7%8B%E6%90%AD%E5%BB%BA">#</a>开始搭建</h3><h4 id="创建一个存放logstash配置的目录并上传配置文件"><a href="#创建一个存放logstash配置的目录并上传配置文件" class="headerlink" title="#创建一个存放logstash配置的目录并上传配置文件"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%AD%98%E6%94%BElogstash%E9%85%8D%E7%BD%AE%E7%9A%84%E7%9B%AE%E5%BD%95%E5%B9%B6%E4%B8%8A%E4%BC%A0%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6">#</a>创建一个存放logstash配置的目录并上传配置文件</h4><h5 id="logstash-springboot-conf文件内容"><a href="#logstash-springboot-conf文件内容" class="headerlink" title="#logstash-springboot.conf文件内容"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#logstash-springboot-conf%E6%96%87%E4%BB%B6%E5%86%85%E5%AE%B9">#</a>logstash-springboot.conf文件内容</h5><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">input &#123;</span><br><span class="line">  tcp &#123;</span><br><span class="line">    mode =&gt; &quot;server&quot;</span><br><span class="line">    host =&gt; &quot;0.0.0.0&quot;</span><br><span class="line">    port =&gt; 4560</span><br><span class="line">    codec =&gt; json_lines</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">output &#123;</span><br><span class="line">  elasticsearch &#123;</span><br><span class="line">    hosts =&gt; &quot;es:9200&quot;</span><br><span class="line">    index =&gt; &quot;springboot-logstash-%&#123;+YYYY.MM.dd&#125;&quot;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="创建配置文件存放目录并上传配置文件到该目录"><a href="#创建配置文件存放目录并上传配置文件到该目录" class="headerlink" title="#创建配置文件存放目录并上传配置文件到该目录"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E5%88%9B%E5%BB%BA%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%AD%98%E6%94%BE%E7%9B%AE%E5%BD%95%E5%B9%B6%E4%B8%8A%E4%BC%A0%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%88%B0%E8%AF%A5%E7%9B%AE%E5%BD%95">#</a>创建配置文件存放目录并上传配置文件到该目录</h5><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mkdir /mydata/logstash</span><br></pre></td></tr></table></figure>

<h4 id="使用docker-compose-yml脚本启动ELK服务"><a href="#使用docker-compose-yml脚本启动ELK服务" class="headerlink" title="#使用docker-compose.yml脚本启动ELK服务"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E4%BD%BF%E7%94%A8docker-compose-yml%E8%84%9A%E6%9C%AC%E5%90%AF%E5%8A%A8elk%E6%9C%8D%E5%8A%A1">#</a>使用docker-compose.yml脚本启动ELK服务</h4><h5 id="docker-compose-yml内容"><a href="#docker-compose-yml内容" class="headerlink" title="#docker-compose.yml内容"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#docker-compose-yml%E5%86%85%E5%AE%B9">#</a>docker-compose.yml内容</h5><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">version: &#x27;3&#x27;</span><br><span class="line">services:</span><br><span class="line">  elasticsearch:</span><br><span class="line">    image: elasticsearch:6.4.0</span><br><span class="line">    container_name: elasticsearch</span><br><span class="line">    environment:</span><br><span class="line">      - &quot;cluster.name=elasticsearch&quot; #设置集群名称为elasticsearch</span><br><span class="line">      - &quot;discovery.type=single-node&quot; #以单一节点模式启动</span><br><span class="line">      - &quot;ES_JAVA_OPTS=-Xms512m -Xmx512m&quot; #设置使用jvm内存大小</span><br><span class="line">    volumes:</span><br><span class="line">      - /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins #插件文件挂载</span><br><span class="line">      - /mydata/elasticsearch/data:/usr/share/elasticsearch/data #数据文件挂载</span><br><span class="line">    ports:</span><br><span class="line">      - 9200:9200</span><br><span class="line">      - 9300:9300</span><br><span class="line">  kibana:</span><br><span class="line">    image: kibana:6.4.0</span><br><span class="line">    container_name: kibana</span><br><span class="line">    links:</span><br><span class="line">      - elasticsearch:es #可以用es这个域名访问elasticsearch服务</span><br><span class="line">    depends_on:</span><br><span class="line">      - elasticsearch #kibana在elasticsearch启动之后再启动</span><br><span class="line">    environment:</span><br><span class="line">      - &quot;elasticsearch.hosts=http://es:9200&quot; #设置访问elasticsearch的地址</span><br><span class="line">    ports:</span><br><span class="line">      - 5601:5601</span><br><span class="line">  logstash:</span><br><span class="line">    image: logstash:6.4.0</span><br><span class="line">    container_name: logstash</span><br><span class="line">    volumes:</span><br><span class="line">      - /mydata/logstash/logstash-springboot.conf:/usr/share/logstash/pipeline/logstash.conf #挂载logstash的配置文件</span><br><span class="line">    depends_on:</span><br><span class="line">      - elasticsearch #kibana在elasticsearch启动之后再启动</span><br><span class="line">    links:</span><br><span class="line">      - elasticsearch:es #可以用es这个域名访问elasticsearch服务</span><br><span class="line">    ports:</span><br><span class="line">      - 4560:4560</span><br></pre></td></tr></table></figure>

<h5 id="上传到linux服务器并使用docker-compose命令运行"><a href="#上传到linux服务器并使用docker-compose命令运行" class="headerlink" title="#上传到linux服务器并使用docker-compose命令运行"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E4%B8%8A%E4%BC%A0%E5%88%B0linux%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%B9%B6%E4%BD%BF%E7%94%A8docker-compose%E5%91%BD%E4%BB%A4%E8%BF%90%E8%A1%8C">#</a>上传到linux服务器并使用docker-compose命令运行</h5><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker-compose up -d</span><br></pre></td></tr></table></figure>

<p>注意：Elasticsearch 启动可能需要好几分钟，要耐心等待。 <img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:50:23_tech_screen_01-52b03edd.png"></p>
<h4 id="在logstash中安装json-lines插件"><a href="#在logstash中安装json-lines插件" class="headerlink" title="#在logstash中安装json_lines插件"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E5%9C%A8logstash%E4%B8%AD%E5%AE%89%E8%A3%85json-lines%E6%8F%92%E4%BB%B6">#</a>在logstash中安装json_lines插件</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"># 进入logstash容器</span><br><span class="line">docker exec -it logstash /bin/bash</span><br><span class="line"># 进入bin目录</span><br><span class="line">cd /bin/</span><br><span class="line"># 安装插件</span><br><span class="line">logstash-plugin install logstash-codec-json_lines</span><br><span class="line"># 退出容器</span><br><span class="line">exit</span><br><span class="line"># 重启logstash服务</span><br><span class="line">docker restart logstash</span><br></pre></td></tr></table></figure>

<h4 id="开启防火墙并在kibana中查看"><a href="#开启防火墙并在kibana中查看" class="headerlink" title="#开启防火墙并在kibana中查看"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E5%BC%80%E5%90%AF%E9%98%B2%E7%81%AB%E5%A2%99%E5%B9%B6%E5%9C%A8kibana%E4%B8%AD%E6%9F%A5%E7%9C%8B">#</a>开启防火墙并在kibana中查看</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl stop firewalld</span><br></pre></td></tr></table></figure>

<p>访问地址：<a target="_blank" rel="noopener" href="http://192.168.3.101:5601/">http://192.168.3.101:5601</a></p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:50:23_tech_screen_02-14052f31.png"></p>
<h2 id="SpringBoot应用集成Logstash"><a href="#SpringBoot应用集成Logstash" class="headerlink" title="#SpringBoot应用集成Logstash"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#springboot%E5%BA%94%E7%94%A8%E9%9B%86%E6%88%90logstash">#</a>SpringBoot应用集成Logstash</h2><h3 id="在pom-xml中添加logstash-logback-encoder依赖"><a href="#在pom-xml中添加logstash-logback-encoder依赖" class="headerlink" title="#在pom.xml中添加logstash-logback-encoder依赖"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E5%9C%A8pom-xml%E4%B8%AD%E6%B7%BB%E5%8A%A0logstash-logback-encoder%E4%BE%9D%E8%B5%96">#</a>在pom.xml中添加logstash-logback-encoder依赖</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--集成logstash--&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;net.logstash.logback&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;logstash-logback-encoder&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;5.3&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>

<h3 id="添加配置文件logback-spring-xml让logback的日志输出到logstash"><a href="#添加配置文件logback-spring-xml让logback的日志输出到logstash" class="headerlink" title="#添加配置文件logback-spring.xml让logback的日志输出到logstash"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E6%B7%BB%E5%8A%A0%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6logback-spring-xml%E8%AE%A9logback%E7%9A%84%E6%97%A5%E5%BF%97%E8%BE%93%E5%87%BA%E5%88%B0logstash">#</a>添加配置文件logback-spring.xml让logback的日志输出到logstash</h3><blockquote>
<p>注意appender节点下的destination需要改成你自己的logstash服务地址，比如我的是：192.168.3.101:4560 。</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE configuration&gt;</span><br><span class="line">&lt;configuration&gt;</span><br><span class="line">    &lt;include resource=&quot;org/springframework/boot/logging/logback/defaults.xml&quot;/&gt;</span><br><span class="line">    &lt;include resource=&quot;org/springframework/boot/logging/logback/console-appender.xml&quot;/&gt;</span><br><span class="line">    &lt;!--应用名称--&gt;</span><br><span class="line">    &lt;property name=&quot;APP_NAME&quot; value=&quot;mall-admin&quot;/&gt;</span><br><span class="line">    &lt;!--日志文件保存路径--&gt;</span><br><span class="line">    &lt;property name=&quot;LOG_FILE_PATH&quot; value=&quot;$&#123;LOG_FILE:-$&#123;LOG_PATH:-$&#123;LOG_TEMP:-$&#123;java.io.tmpdir:-/tmp&#125;&#125;&#125;/logs&#125;&quot;/&gt;</span><br><span class="line">    &lt;contextName&gt;$&#123;APP_NAME&#125;&lt;/contextName&gt;</span><br><span class="line">    &lt;!--每天记录日志到文件appender--&gt;</span><br><span class="line">    &lt;appender name=&quot;FILE&quot; class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;</span><br><span class="line">        &lt;rollingPolicy class=&quot;ch.qos.logback.core.rolling.TimeBasedRollingPolicy&quot;&gt;</span><br><span class="line">            &lt;fileNamePattern&gt;$&#123;LOG_FILE_PATH&#125;/$&#123;APP_NAME&#125;-%d&#123;yyyy-MM-dd&#125;.log&lt;/fileNamePattern&gt;</span><br><span class="line">            &lt;maxHistory&gt;30&lt;/maxHistory&gt;</span><br><span class="line">        &lt;/rollingPolicy&gt;</span><br><span class="line">        &lt;encoder&gt;</span><br><span class="line">            &lt;pattern&gt;$&#123;FILE_LOG_PATTERN&#125;&lt;/pattern&gt;</span><br><span class="line">        &lt;/encoder&gt;</span><br><span class="line">    &lt;/appender&gt;</span><br><span class="line">    &lt;!--输出到logstash的appender--&gt;</span><br><span class="line">    &lt;appender name=&quot;LOGSTASH&quot; class=&quot;net.logstash.logback.appender.LogstashTcpSocketAppender&quot;&gt;</span><br><span class="line">        &lt;!--可以访问的logstash日志收集端口--&gt;</span><br><span class="line">        &lt;destination&gt;192.168.3.101:4560&lt;/destination&gt;</span><br><span class="line">        &lt;encoder charset=&quot;UTF-8&quot; class=&quot;net.logstash.logback.encoder.LogstashEncoder&quot;/&gt;</span><br><span class="line">    &lt;/appender&gt;</span><br><span class="line">    &lt;root level=&quot;INFO&quot;&gt;</span><br><span class="line">        &lt;appender-ref ref=&quot;CONSOLE&quot;/&gt;</span><br><span class="line">        &lt;appender-ref ref=&quot;FILE&quot;/&gt;</span><br><span class="line">        &lt;appender-ref ref=&quot;LOGSTASH&quot;/&gt;</span><br><span class="line">    &lt;/root&gt;</span><br><span class="line">&lt;/configuration&gt;</span><br></pre></td></tr></table></figure>

<h3 id="运行Springboot应用"><a href="#运行Springboot应用" class="headerlink" title="#运行Springboot应用"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E8%BF%90%E8%A1%8Cspringboot%E5%BA%94%E7%94%A8">#</a>运行Springboot应用</h3><p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:50:23_tech_screen_03-6e5e33d4.png"></p>
<h2 id="在kibana中查看日志信息"><a href="#在kibana中查看日志信息" class="headerlink" title="#在kibana中查看日志信息"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E5%9C%A8kibana%E4%B8%AD%E6%9F%A5%E7%9C%8B%E6%97%A5%E5%BF%97%E4%BF%A1%E6%81%AF">#</a>在kibana中查看日志信息</h2><h3 id="创建index-pattern"><a href="#创建index-pattern" class="headerlink" title="#创建index pattern"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E5%88%9B%E5%BB%BAindex-pattern">#</a>创建index pattern</h3><p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:50:23_tech_screen_04-392f4d69.png"></p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:50:23_tech_screen_05-038f1c0a.png"></p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:50:23_tech_screen_06-b5ff7766.png"></p>
<h3 id="查看收集的日志"><a href="#查看收集的日志" class="headerlink" title="#查看收集的日志"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E6%9F%A5%E7%9C%8B%E6%94%B6%E9%9B%86%E7%9A%84%E6%97%A5%E5%BF%97">#</a>查看收集的日志</h3><p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:50:23_tech_screen_07-49481d40.png"></p>
<h3 id="调用接口进行测试"><a href="#调用接口进行测试" class="headerlink" title="#调用接口进行测试"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E8%B0%83%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%BF%9B%E8%A1%8C%E6%B5%8B%E8%AF%95">#</a>调用接口进行测试</h3><p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:50:23_tech_screen_08-0208beae.png"></p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:50:23_tech_screen_09-688d0645.png"></p>
<h3 id="制造一个异常并查看"><a href="#制造一个异常并查看" class="headerlink" title="#制造一个异常并查看"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E5%88%B6%E9%80%A0%E4%B8%80%E4%B8%AA%E5%BC%82%E5%B8%B8%E5%B9%B6%E6%9F%A5%E7%9C%8B">#</a>制造一个异常并查看</h3><h4 id="修改获取所有品牌列表接口"><a href="#修改获取所有品牌列表接口" class="headerlink" title="#修改获取所有品牌列表接口"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E4%BF%AE%E6%94%B9%E8%8E%B7%E5%8F%96%E6%89%80%E6%9C%89%E5%93%81%E7%89%8C%E5%88%97%E8%A1%A8%E6%8E%A5%E5%8F%A3">#</a>修改获取所有品牌列表接口</h4><p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:50:23_tech_screen_10-ad07daa0.png"></p>
<h4 id="调用该接口并查看日志"><a href="#调用该接口并查看日志" class="headerlink" title="#调用该接口并查看日志"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E8%B0%83%E7%94%A8%E8%AF%A5%E6%8E%A5%E5%8F%A3%E5%B9%B6%E6%9F%A5%E7%9C%8B%E6%97%A5%E5%BF%97">#</a>调用该接口并查看日志</h4><p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:50:23_tech_screen_11-63b5329a.png"></p>
<h3 id="总结"><a href="#总结" class="headerlink" title="#总结"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E6%80%BB%E7%BB%93">#</a>总结</h3><p>搭建了ELK日志收集系统之后，我们如果要查看SpringBoot应用的日志信息，就不需要查看日志文件了，直接在Kibana中查看即可。</p>
<h2 id="项目源码地址"><a href="#项目源码地址" class="headerlink" title="#项目源码地址"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/mall_tiny_elk.html#%E9%A1%B9%E7%9B%AE%E6%BA%90%E7%A0%81%E5%9C%B0%E5%9D%80">#</a>项目源码地址</h2><p><a target="_blank" rel="noopener" href="https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-elk">https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-elk</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://ko25891wan.gitlab.io">十一星野</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://ko25891wan.gitlab.io/2024/01/27851c2fc81c.html">https://ko25891wan.gitlab.io/2024/01/27851c2fc81c.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://ko25891wan.gitlab.io" target="_blank">小小程序员</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2024/01/ae834480f808.html" title="mall-swarm使用Jenkins实现自动化部署"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">mall-swarm使用Jenkins实现自动化部署</div></div></a></div><div class="next-post pull-right"><a href="/2024/01/42a7eaf90bb7.html" title="Spring Data Redis 最佳实践！"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Spring Data Redis 最佳实践！</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">十一星野</div><div class="author-info__description">归途也还可爱</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">120</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io"><i class="fab fa-github"></i><span>关注我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io" target="_blank" title="Gitlab"><i class="fab fa-gitlab" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:ko25891wan@outlook.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%A6%E4%B9%A0%E5%89%8D%E9%9C%80%E8%A6%81%E4%BA%86%E8%A7%A3%E7%9A%84%E5%86%85%E5%AE%B9"><span class="toc-number">1.</span> <span class="toc-text">学习前需要了解的内容</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ELK%E4%B8%AD%E5%90%84%E4%B8%AA%E6%9C%8D%E5%8A%A1%E7%9A%84%E4%BD%9C%E7%94%A8"><span class="toc-number">2.</span> <span class="toc-text">ELK中各个服务的作用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8Docker-Compose-%E6%90%AD%E5%BB%BAELK%E7%8E%AF%E5%A2%83"><span class="toc-number">3.</span> <span class="toc-text">使用Docker Compose 搭建ELK环境</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9C%80%E8%A6%81%E4%B8%8B%E8%BD%BD%E7%9A%84Docker%E9%95%9C%E5%83%8F"><span class="toc-number">3.1.</span> <span class="toc-text">需要下载的Docker镜像</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%90%AD%E5%BB%BA%E5%89%8D%E5%87%86%E5%A4%87"><span class="toc-number">3.2.</span> <span class="toc-text">搭建前准备</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%80%E5%A7%8B%E6%90%AD%E5%BB%BA"><span class="toc-number">3.3.</span> <span class="toc-text">开始搭建</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%AD%98%E6%94%BElogstash%E9%85%8D%E7%BD%AE%E7%9A%84%E7%9B%AE%E5%BD%95%E5%B9%B6%E4%B8%8A%E4%BC%A0%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">3.3.1.</span> <span class="toc-text">创建一个存放logstash配置的目录并上传配置文件</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#logstash-springboot-conf%E6%96%87%E4%BB%B6%E5%86%85%E5%AE%B9"><span class="toc-number">3.3.1.1.</span> <span class="toc-text">logstash-springboot.conf文件内容</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%AD%98%E6%94%BE%E7%9B%AE%E5%BD%95%E5%B9%B6%E4%B8%8A%E4%BC%A0%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%88%B0%E8%AF%A5%E7%9B%AE%E5%BD%95"><span class="toc-number">3.3.1.2.</span> <span class="toc-text">创建配置文件存放目录并上传配置文件到该目录</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8docker-compose-yml%E8%84%9A%E6%9C%AC%E5%90%AF%E5%8A%A8ELK%E6%9C%8D%E5%8A%A1"><span class="toc-number">3.3.2.</span> <span class="toc-text">使用docker-compose.yml脚本启动ELK服务</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#docker-compose-yml%E5%86%85%E5%AE%B9"><span class="toc-number">3.3.2.1.</span> <span class="toc-text">docker-compose.yml内容</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%B8%8A%E4%BC%A0%E5%88%B0linux%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%B9%B6%E4%BD%BF%E7%94%A8docker-compose%E5%91%BD%E4%BB%A4%E8%BF%90%E8%A1%8C"><span class="toc-number">3.3.2.2.</span> <span class="toc-text">上传到linux服务器并使用docker-compose命令运行</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9C%A8logstash%E4%B8%AD%E5%AE%89%E8%A3%85json-lines%E6%8F%92%E4%BB%B6"><span class="toc-number">3.3.3.</span> <span class="toc-text">在logstash中安装json_lines插件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BC%80%E5%90%AF%E9%98%B2%E7%81%AB%E5%A2%99%E5%B9%B6%E5%9C%A8kibana%E4%B8%AD%E6%9F%A5%E7%9C%8B"><span class="toc-number">3.3.4.</span> <span class="toc-text">开启防火墙并在kibana中查看</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#SpringBoot%E5%BA%94%E7%94%A8%E9%9B%86%E6%88%90Logstash"><span class="toc-number">4.</span> <span class="toc-text">SpringBoot应用集成Logstash</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8pom-xml%E4%B8%AD%E6%B7%BB%E5%8A%A0logstash-logback-encoder%E4%BE%9D%E8%B5%96"><span class="toc-number">4.1.</span> <span class="toc-text">在pom.xml中添加logstash-logback-encoder依赖</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6logback-spring-xml%E8%AE%A9logback%E7%9A%84%E6%97%A5%E5%BF%97%E8%BE%93%E5%87%BA%E5%88%B0logstash"><span class="toc-number">4.2.</span> <span class="toc-text">添加配置文件logback-spring.xml让logback的日志输出到logstash</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%90%E8%A1%8CSpringboot%E5%BA%94%E7%94%A8"><span class="toc-number">4.3.</span> <span class="toc-text">运行Springboot应用</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%A8kibana%E4%B8%AD%E6%9F%A5%E7%9C%8B%E6%97%A5%E5%BF%97%E4%BF%A1%E6%81%AF"><span class="toc-number">5.</span> <span class="toc-text">在kibana中查看日志信息</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BAindex-pattern"><span class="toc-number">5.1.</span> <span class="toc-text">创建index pattern</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E6%94%B6%E9%9B%86%E7%9A%84%E6%97%A5%E5%BF%97"><span class="toc-number">5.2.</span> <span class="toc-text">查看收集的日志</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%B0%83%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%BF%9B%E8%A1%8C%E6%B5%8B%E8%AF%95"><span class="toc-number">5.3.</span> <span class="toc-text">调用接口进行测试</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%B6%E9%80%A0%E4%B8%80%E4%B8%AA%E5%BC%82%E5%B8%B8%E5%B9%B6%E6%9F%A5%E7%9C%8B"><span class="toc-number">5.4.</span> <span class="toc-text">制造一个异常并查看</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9%E8%8E%B7%E5%8F%96%E6%89%80%E6%9C%89%E5%93%81%E7%89%8C%E5%88%97%E8%A1%A8%E6%8E%A5%E5%8F%A3"><span class="toc-number">5.4.1.</span> <span class="toc-text">修改获取所有品牌列表接口</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%B0%83%E7%94%A8%E8%AF%A5%E6%8E%A5%E5%8F%A3%E5%B9%B6%E6%9F%A5%E7%9C%8B%E6%97%A5%E5%BF%97"><span class="toc-number">5.4.2.</span> <span class="toc-text">调用该接口并查看日志</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">5.5.</span> <span class="toc-text">总结</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%A1%B9%E7%9B%AE%E6%BA%90%E7%A0%81%E5%9C%B0%E5%9D%80"><span class="toc-number">6.</span> <span class="toc-text">项目源码地址</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/c60470b9a892.html" title="Java集合使用注意事项总结">Java集合使用注意事项总结</a><time datetime="2024-02-03T04:58:22.000Z" title="发表于 2024-02-03 12:58:22">2024-02-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/81fb7a201f29.html" title="无题">无题</a><time datetime="2024-02-03T03:15:31.812Z" title="发表于 2024-02-03 11:15:31">2024-02-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/5737c1ec69a9.html" title="Wed Jan 17 2024 00:00:00 GMT+0800 (中国标准时间)">Wed Jan 17 2024 00:00:00 GMT+0800 (中国标准时间)</a><time datetime="2024-01-31T06:56:16.000Z" title="发表于 2024-01-31 14:56:16">2024-01-31</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/f510db7d2f19.html" title="Spring常见面试题总结">Spring常见面试题总结</a><time datetime="2024-01-31T06:55:58.000Z" title="发表于 2024-01-31 14:55:58">2024-01-31</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/9be065e9f288.html" title="redis 面试">redis 面试</a><time datetime="2024-01-31T06:55:55.000Z" title="发表于 2024-01-31 14:55:55">2024-01-31</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2024 By 十一星野</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.staticfile.org/fancyapps-ui/5.0.32/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.staticfile.org/instant.page/5.2.0/instantpage.min.js" type="module"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>